<template>
	<view>
		<view class="header">
			<image @click="back" class="back" src="../../static/tian/back.png" mode=""></image>
			<view class="text">{{$t('wallet.exchange')}}</view>
			<view style="width: 44rpx;"></view>
		</view>
		<view style="height: 104rpx;"></view>
		<view class="item">
			<view class="title">{{$t('wallet.Send')}}</view>
			<view class="item2">
				<view style="width:25%;height: 100%;" class="flex" @click="show3 = true">
					<img :src="baseurl + select.logo" alt="" style="width: 32rpx;height: 32rpx;">
					<view style="margin: 0 10rpx;">{{select.code}}</view>
					<img src="../../static/img/xia.png" alt="" style="width: 16rpx;height: 16rpx;">
				</view>
				<u-input style="border: 0;" :placeholder="$t('wallet.least10')" v-model="number" @input="jisuan"></u-input>
				<text style="color: #ffdb8b;font-weight: 800;" @click="max">{{$t('wallet.more')}}</text>
			</view>
			<view class="Available">
				<text>{{$t('wallet.availableBalance')}}：{{select.balance}} {{select.code}}</text>
				<text>{{$t('wallet.commission')}}：0%</text>
			</view>
		</view>
		<view class="item">
			<view class="title">{{$t('wallet.Receive')}}</view>
			<view class="item2">
				<view style="width:25%;height: 100%;" class="flex" @click="show = true">
					<img :src="baseurl + select1.logo" alt="" style="width: 32rpx;height: 32rpx;">
					<view style="margin: 0 10rpx;">{{select1.code}}</view>
					<img src="../../static/img/xia.png" alt="" style="width: 16rpx;height: 16rpx;">
				</view>
				<view v-if="numbers == ''" class="input">{{$t('first.title110')}}</view>
				<view v-else class="input" style="color: rgb(48, 49, 51);">{{numbers}}</view>
			</view>
		</view>
		<view class="txBtn" @click="exchange">{{$t('wallet.ImmediateExchange')}}</view>
		<view class="balance">
			<view style="font-weight:800 ;">{{$t('wallet.ExchangeRecord')}}</view>
			<view class="nodata" style="margin-top: 200rpx;" v-if="!detailist">
				<image class="icon" src="../../static/tian/zanwu.png" mode=""></image>
				<view class="nodata_text">{{$t('first.title44')}}</view>
			</view>
			
			<view class="item3" v-if="detailist" v-for="(item,index) in detailist">
				<view class="itemheader" style="margin-bottom: 18rpx;">
					<view class="left">{{item.number}}{{item.buy_to_currency}}</view>
					<view v-if="item.status==0" class="right">
						
						{{$t('first.title61')}}
					</view>
					<view v-if="item.status==1" class="right2">
						{{$t('first.title61')}}
					</view>
				</view>
				<view class="itemheader">
					<view class="left1">{{item.buy_time}}</view>
					<view class="right1">REAL: {{item.real}}</view>
				</view>
			</view>
			<u-loadmore
				v-if="detailist"
				:status="loadmorestatus" 
				:loading-text="$t('first.title120')" 
				:loadmore-text="$t('first.title119')" 
				:nomore-text="$t('first.title121')"
				marginTop="18px"
			/>
		</view>
		<u-picker :show="show"  keyName="code" :columns="columns" @cancel="show = false" @confirm="confirm" cancelText="cancel" confirmText="OK"></u-picker>
		<u-picker :show="show3" keyName="code" cancelText="cancel" confirmText="OK"
			:columns="columns2" @cancel="show3 = false" @confirm="confirm1"></u-picker>
	</view>
</template>

<script>
	import {
		domain
	} from '../../config/domain.js'
	export default {
		data() {
			return {
				show: false,
				show2: false,
				show3: false,
				columns: [],
				columns2: [],
				radiolist: [{
						name: 'BSC',
						disabled: false
					},
					{
						name: 'ERC-20',
						disabled: false
					},
					{
						name: 'MATIC',
						disabled: false
					}, {
						name: 'ARB',
						disabled: false
					},
					{
						name: 'OP',
						disabled: false
					}
				],
				radiovalue1: 'BSC',
				value: '',
				select:[],
				select1:[],
				baseurl:'',
				number:'',
				detailist:[],
				numbers:'',
				
				page:1,
				lastpage:1,
				loadmorestatus:'loadmore'
			};
		},
		onLoad(){
			uni.setNavigationBarTitle({
				title: this.$t('wallet.exchange')
			});
			this.baseurl = domain;
			this.getlist();
			this.getlist3()
		},
		onReachBottom() {
			this.page++
			if(this.page <= this.lastpage){
				this.loadmorestatus = 'loading'
				var url = domain + '/api/flash/list?page='+this.page;
				var mytype = 'get';
				var userinfo = uni.getStorageSync('userinfo');
				uni.request({
					url,
					method: mytype,
					header: {
						'content-type': 'application/x-www-form-urlencoded', //自定义请求头信息
						'Lang': uni.getStorageSync('lang'),
						'AUTHORIZATION': userinfo.token,
					},
					success: res => {
						this.detailist=[...this.detailist,...res.data.data.data]
						this.loadmorestatus = 'loadmore'
					},
					fail: (err) => {
						this.loadmorestatus = 'loadmore'
						console.log(err)
					},
					complete: () => {
						this.loadmorestatus = 'loadmore'
						setTimeout(function() {
							uni.hideLoading();
						}, 1000);
					}
				});
			}else{
				this.loadmorestatus = 'nomore'
			}
		},
		methods: {
			jisuan(e){
				this.numbers = (e*(this.select.price/this.select1.price)*1).toFixed(4);
			},
			exchange(){
				if (!this.number) {
					uni.showToast({
						icon:'none',
						title:this.$t('wallet.Please') +  this.$t('first.title98')
					}); 
					return false;
				}
				
				var url = domain + 'api/flash/add';
				var mytype = 'post';
				var userinfo = uni.getStorageSync('userinfo');
				var data={
					currency_id:this.select.id,
					number:this.number,
					currency_id2:this.select1.id,
				};
				uni.request({
					url,
					method: mytype,
					data,
					header: {
						'content-type': 'application/x-www-form-urlencoded' ,//自定义请求头信息
						'Lang':uni.getStorageSync('lang'),
						'AUTHORIZATION':userinfo.token,
					},
					success: res => {
						if(res.data.code > 0){
							this.getlist3()
							this.number = '';
							this.numbers = '';
						}
							uni.showToast({
								icon:'none',
								title:res.data.msg
							}); 
						
					},
					fail: (err) => {
						console.log(err)
					},
					complete: () => {
						setTimeout(function() {
							uni.hideLoading();
						}, 1000);
					}
				});
			},
			
			
			max(){
				this.number = this.select.balance
			},
			getlist(){
				var url = domain + 'api/user/currency?type=1';
				var mytype = 'get';
				var userinfo = uni.getStorageSync('userinfo');
				uni.request({
					url,
					method: mytype,
					header: {
						'content-type': 'application/x-www-form-urlencoded' ,//自定义请求头信息
						'Lang':uni.getStorageSync('lang'),
						'AUTHORIZATION':userinfo.token,
					},
					success: res => {
						let array = [];
						res.data.list2.forEach((v, i) => {
							if(v.code != 'DKC'){
								v.code = v.code + 'D'
							}else{
								v.code = v.code
							}
							array.push(v)
						})
						this.columns2.push(array)
						this.select = this.columns2[0][0]
						
						this.getlist2();
					},
					fail: (err) => {
						console.log(err)
					},
					// complete: () => {
					// 	setTimeout(function() {
					// 		uni.hideLoading();
					// 	}, 1000);
					// }
				});
			},
			confirm(e){
				this.select1 = e.value[0];
				this.numbers = (this.number*(this.select.price/this.select1.price)*1).toFixed(4);
				this.show = false;
			},
			confirm1(e) {
				this.select = e.value[0];
				this.number = '';
				this.numbers = '';
				this.show3 = false;
				this.getlist2();
			},
			getlist2(){
				var url = domain + '/api/flash/currency?currency_id=' + this.select.id;
				var mytype = 'get';
				var userinfo = uni.getStorageSync('userinfo');
				// uni.showLoading({
				// 	title: '加载中',
				// 	mask: true
				// });
				// uni.showLoading({});
				uni.request({
					url,
					method: mytype,
					header: {
						'content-type': 'application/x-www-form-urlencoded', //自定义请求头信息
						'Lang': uni.getStorageSync('lang'),
						'AUTHORIZATION': userinfo.token,
					},
					success: res => {
						this.columns = [];
						let array = [];
						res.data.data.forEach((v, i) => {

							v.code = v.code

							array.push(v)
						})
						this.columns.push(array)
						this.select1 = this.columns[0][0];
						
						// uni.hideLoading();
					},
					fail: (err) => {
						console.log(err)
					},
					// complete: () => {
					// 	setTimeout(function() {
					// 		uni.hideLoading();
					// 	}, 1000);
					// }
				});
			},
			getlist3(){
				var url = domain + '/api/flash/list?page='+this.page;
				var mytype = 'get';
				var userinfo = uni.getStorageSync('userinfo');
				uni.request({
					url,
					method: mytype,
					header: {
						'content-type': 'application/x-www-form-urlencoded', //自定义请求头信息
						'Lang': uni.getStorageSync('lang'),
						'AUTHORIZATION': userinfo.token,
					},
					success: res => {
						this.detailist=res.data.data.data
						this.lastpage =res.data.data.last_page
						if(this.lastpage == 1){
							this.loadmorestatus = 'nomore'
						}
					},
					fail: (err) => {
						console.log(err)
					},
					complete: () => {
						setTimeout(function() {
							uni.hideLoading();
						}, 1000);
					}
				});
			},
			groupChange() {

			},
			back() {
				wx.navigateBack()
			},
		}
	}
</script>
<style>
	.header {
		width: 100%;
		height: 104rpx;
		position: fixed;
		background-color: #ecf0f5;
		display: flex;
		top: 0;
		left: 0;
		align-items: center;
		justify-content: space-between;
		z-index: 999;
	}

	.header image {
		width: 48rpx;
		height: 48rpx;
	}

	.header .text {
		font-size: 36rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 600;
		color: #222;
	}

	.back {
		margin-left: 20rpx;
	}

	.time {
		margin-right: 20rpx;
	}
</style>
<style lang="scss">
	page {
		background-color: #ecf0f5;
	}

	.flex {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.item {
		width: 90%;
		padding: 20rpx;
		margin: 20rpx auto 50rpx;

		.title {
			font-size: 30rpx;
			font-family: PingFangSC-Semibold, PingFang SC;
			font-weight: 600;
			color: #8287a2;

		}


		.item2 {
			margin-top: 15px;
			background-color: #fff;
			height: 70rpx;
			border-radius: 20rpx;
			width: 90%;
			display: flex;
			align-items: center;
			padding: 0 20rpx;
			
			.input{
				font-size: 26rpx;
				margin-left: 20rpx;
				color: rgb(192, 196, 204);
			}
		}

		.Available {
			margin-top: 20rpx;
			font-size: 24rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			color: #a6a6a6
		}

	}

	.txBtn {
		width: 90%;
		height: 96rpx;
		margin: 50rpx auto;
		background: #ffb105;
		border-radius: 50rpx;
		font-size: 36rpx;
		font-weight: 600;
		color: #000;
		text-align: center;
		line-height: 96rpx;
	}

	.uInput {
		background-color: #fff;
		margin-top: 10rpx;
		width: 90%;
		border-radius: 20rpx;
	}

	.balance {
		margin-top: 50rpx;
		width: 100%;
		box-shadow: 26rpx 26rpx 0 #f1f4f6;
		border-radius: 50rpx 50rpx 0 0;
		padding: 50rpx;
		box-sizing: border-box;
		background-color: #fff;
	}
	
	.nodata{
		width: 100%;
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
	}
	.nodata .icon{
		width: 420rpx;
		height: 366rpx;
	}
	.nodata_text{
		font-size: 28rpx;
		font-family: PingFangSC-Regular,PingFang SC;
		font-weight: 400;
		color: #9394bc;
	}
	.item3{
		width: 100%;
		height: 146rpx;
		background: #fff;
		box-shadow: 0 0.18667rem 0.42667rem 0 #ddd;
		border-radius: 32rpx;
		margin-top: 40rpx;
		padding: 36rpx;
		box-sizing: border-box;
	}
	.itemheader{
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.itemheader .left{
		font-size: 32rpx;
		font-weight: bold;
		color: #323233;
	}
	.itemheader .left1{
		font-size: 24rpx;
		color: #858587;
	}
	.itemheader .right{
		font-size: 28rpx;
		color: #ffb40f;
	}
	.itemheader .right1{
		font-size: 24rpx;
		color: #858587;
	}
</style>